package com.tanhua.server.service;

/**
 * @Author: cl
 * @Date: 2022/5/17 - 05 - 17
 * @Description:
 */

import com.alibaba.fastjson.JSON;
import com.tanhua.commons.utils.Constants;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.server.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * 用户冻结服务(登录、发动态、评论)
 */
@Service
public class UserFreezeService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    public void checkUserStatus(Integer state, Long userId) {

        //1、获取redis中是否包含冻结数据
        String value = redisTemplate.opsForValue().get(Constants.FREEZE_USER + userId);
        //2、判断是否有数据
        if (!StringUtils.isEmpty(value)) {
            Map map = JSON.parseObject(value, Map.class);
            Integer freezingRange = Integer.parseInt(map.get("freezingRange").toString()) ;//冻结分类
            //3、判断是哪种类型的冻结
            if (freezingRange == state) {
                if (state == 1) {
                    throw new BusinessException(ErrorResult.builder().errMessage("您的账户登录被冻结").build());
                }
                if (state == 2) {
                    throw new BusinessException(ErrorResult.builder().errMessage("您的账户评论被冻结").build());
                }
                if (state == 3) {
                    throw new BusinessException(ErrorResult.builder().errMessage("您的账户发布动态被冻结").build());
                }
            }
        }
    }
}
